package com.huawei.fastengine.fastview.download.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.text.TextUtils;
import android.util.Base64;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

public class CheckUtils {
    private static final String FASTAPP_ENGINE_SIGN = "388dec495f1e93fffc91436e285a0d89b1499269b071d33ebbc7332ca7a2d426";
    private static final String TAG = "Utils";

    private static String bytesToHexString(byte[] bArr) {
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bArr) {
            String toHexString = Integer.toHexString(b & 255);
            if (toHexString.length() == 1) {
                stringBuilder.append('0');
            }
            stringBuilder.append(toHexString);
        }
        return stringBuilder.toString();
    }

    private static String handleSHA256(String str) {
        String str2 = "";
        try {
            MessageDigest instance = MessageDigest.getInstance("SHA256");
            instance.update(str.getBytes("UTF-8"));
            str2 = bytesToHexString(instance.digest());
        } catch (NoSuchAlgorithmException e) {
            FastViewLogUtils.w(TAG, "NoSuchAlgorithmException!");
        } catch (UnsupportedEncodingException e2) {
            FastViewLogUtils.w(TAG, "UnsupportedEncodingException!");
        }
        return str2;
    }

    public static boolean isCorrectSign(Context context, String str) {
        if (context == null || TextUtils.isEmpty(str)) {
            return false;
        }
        PackageManager packageManager = context.getPackageManager();
        if (packageManager == null) {
            FastViewLogUtils.e(TAG, "isCorrectSign PackageManager null!");
            return false;
        }
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(str, 64);
            if (packageInfo == null) {
                FastViewLogUtils.e(TAG, "isCorrectSign PackageInfo null!");
                return false;
            }
            Signature[] signatureArr = packageInfo.signatures;
            if (signatureArr == null) {
                FastViewLogUtils.e(TAG, "isCorrectSign signs null!");
                return false;
            }
            Object obj = "";
            if (signatureArr[0] != null) {
                obj = handleSHA256(Base64.encodeToString(((X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(signatureArr[0].toByteArray()))).getPublicKey().getEncoded(), 0));
            }
            return FASTAPP_ENGINE_SIGN.equals(obj);
        } catch (NameNotFoundException e) {
            FastViewLogUtils.e(TAG, "isCorrectSign PackageManager.NameNotFoundException!");
            return false;
        } catch (CertificateException e2) {
            FastViewLogUtils.e(TAG, "isCorrectSign CertificateException!");
            return false;
        }
    }
}
